package roof_extractor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.NodeList;

public class ImmobiliareItAnnuncio implements Annuncio {

	private Map<String,String> keyValues;
	private String filePath;
	
	private String getContent(NodeList nodes){
		String content =  (String) nodes.item(0).getNodeValue();
		if(nodes.getLength()>1){
			for(int i=1;i<nodes.getLength();i++){ 
				content = content.concat(nodes.item(i).getNodeValue() + " ");	
			}
		}
		return content;
	}
	
	public ImmobiliareItAnnuncio(File file) throws IOException{
		
		this.keyValues = new HashMap<String, String>();
		this.filePath = file.getAbsolutePath();
		
		try {
			
			Pattern pattern = Pattern.compile("<title>(.*) in (.*) a (.*) - immobiliare.it</title>");
			Matcher matcher = pattern.matcher(Utils.fromFile(file.getAbsolutePath()));
			if(matcher.find()){
				this.keyValues.put("tipologia",matcher.group(1));
				this.keyValues.put("contratto",matcher.group(2));
				this.keyValues.put("comune",matcher.group(3));
			}
			
			this.keyValues.put("prezzo", this.getContent(Utils.getXPathFromFile("//node()[@id='prezzoImmobile']//text()",file)).replaceAll("[^0-9]", "") );
			this.keyValues.put("descrizione", this.getContent(Utils.getXPathFromFile("//node()[@id='tabcontent_it']//text()",file)).toLowerCase());
			
			pattern = Pattern.compile("<strong>Locali:&nbsp;</strong>(\\d+)&nbsp;");
			matcher = pattern.matcher(Utils.fromFile(file.getAbsolutePath()));
			if(matcher.find()){
				this.keyValues.put("locali",matcher.group(1));
			}
			
			// indirizzoHighLight +="via michelangelo poggioli"
			pattern = Pattern.compile("indirizzoHighLight \\+?=\"([^\"]+)\"");
			matcher = pattern.matcher(Utils.fromFile(file.getAbsolutePath()));
			while(matcher.find()){
				this.keyValues.put("comune",this.keyValues.get("comune").concat(" - " + matcher.group(1)));
			}
			
			pattern = Pattern.compile("<strong>mq:&nbsp;</strong>(\\d+)&nbsp;");
			matcher = pattern.matcher(Utils.fromFile(file.getAbsolutePath()));
			if(matcher.find()){
				this.keyValues.put("superficie",matcher.group(1));
			}
			
			String agenzia = "";
			pattern = Pattern.compile("<span class=\"tel fisso\" style=\"width:70px;\">(\\d+)</span>");
			matcher = pattern.matcher(Utils.fromFile(file.getAbsolutePath()));
			if(matcher.find()){ agenzia = agenzia.concat(" Tel: " + matcher.group(1)); }
			pattern = Pattern.compile("<span class=\"tel cell\" style=\"width:70px;\">(\\d+)</span>");
			matcher = pattern.matcher(Utils.fromFile(file.getAbsolutePath()));
			if(matcher.find()){ agenzia = agenzia.concat(" Mobile: " + matcher.group(1)); }
			pattern = Pattern.compile("<span class=\"tel fax\" style=\"width:70px;\">(\\d+)</span>");
			matcher = pattern.matcher(Utils.fromFile(file.getAbsolutePath()));
			if(matcher.find()){ agenzia = agenzia.concat(" Fax: " + matcher.group(1)); }
			agenzia = agenzia.trim();
			if(!agenzia.equals("")){ this.keyValues.put("agenzia",agenzia); }
			
			pattern = Pattern.compile("var latitudine = \"(\\d+)\\.(\\d+)\";");
			matcher = pattern.matcher(Utils.fromFile(file.getAbsolutePath()));
			if(matcher.find()){
				this.keyValues.put("latitudine",matcher.group(1)+"."+matcher.group(2));
			}
			pattern = Pattern.compile("var longitudine = \"(\\d+)\\.(\\d+)\";");
			matcher = pattern.matcher(Utils.fromFile(file.getAbsolutePath()));
			if(matcher.find()){
				this.keyValues.put("longitudine",matcher.group(1)+"."+matcher.group(2));
			}
			
			
		} catch (FileNotFoundException e) {
			System.out.println("File Not Found !!! [ " + file.getAbsolutePath() + " ]");
		} catch (XPathExpressionException e) {
			e.printStackTrace();
		}

	}
	
	public String getFilePath(){
		return this.filePath;
	}
	
	public String getPrezzo() {
		return this.keyValues.get("prezzo");
	}
	public String getLocali() {
		return this.keyValues.get("locali");
	}
	public String getSuperficie() {
		return this.keyValues.get("superficie");
	}
	public String getIndirizzo() {
		return this.keyValues.get("comune");
	}
	public String getTipologia() {
		return this.keyValues.get("tipologia");
	}
	public String getContratto() {
		return this.keyValues.get("contratto");
	}
	public String getDescrizione() {
		return this.keyValues.get("descrizione");
	}
	public String getContatti() {
		return this.keyValues.get("agenzia");
	}
	public String getLatitudine(){
		return this.keyValues.get("latitudine");
	}
	public String getLongitudine(){
		return this.keyValues.get("longitudine");
	}
	
	public void printData(){
		
		System.out.println("-----------------------------------");
		System.out.println(">>> Annuncio "+ this.getFilePath() +" <<<");
		System.out.println("Contratto: "   + this.getContratto());
		System.out.println("Tipologia: "   + this.getTipologia());
		System.out.println("Indirizzo: "   + this.getIndirizzo());
		System.out.println("Coordinate: "  + this.getLatitudine()+" - "+this.getLongitudine());
		System.out.println("Locali: "      + this.getLocali());
		System.out.println("Superficie: "  + this.getSuperficie());
		System.out.println("Prezzo: "      + this.getPrezzo());
		System.out.println("Descrizione: " + this.getDescrizione());
		System.out.println("Contatti: "    + this.getContatti());
		System.out.println("-----------------------------------");
	}

	public void save(String db_abs_filepath) throws ClassNotFoundException, SQLException {
		Utils.saveAnnuncio(db_abs_filepath,this);
		
	}

}
